import Deng from '@/views/deng.vue'
import Cart from '@/views/home/cart.vue'
import Cla from '@/views/home/cla.vue'
import Home from '@/views/home/home.vue'
import Index from '@/views/home/index.vue'
import My from '@/views/home/my.vue'
import Zhu from '@/views/zhu.vue'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/home/index' },
    {
      path: '/home',
      component: Home,
      children: [
        {
          path: 'index',
          component: Index,
        },
        {
          path: 'cla',
          component: Cla,
        },
        {
          path: 'cart',
          component: Cart,
        },
        {
          path: 'my',
          component: My,
        },

      ]
    },
       {
          path: '/deng',
          component: Deng,
        },
       {
          path: '/zhu',
          component: Zhu,
        },
  ],
})

router.beforeEach((to, from, next) => {
  if (to.fullPath == '/home/cart' || to.fullPath == '/home/my') {
    let token = localStorage.getItem('token')
    if (token) {
      next()
    }
    else {
      next('/zhu')
    }
  }
  else {
    next()
  }
})

export default router
